package icu.groot.alan.hotel.controller.room;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import icu.groot.alan.hotel.entity.dao.OrderDo;
import icu.groot.alan.hotel.entity.dao.RoomDo;
import icu.groot.alan.hotel.enums.OrderStatusEnum;
import icu.groot.alan.hotel.service.OrderService;
import icu.groot.alan.hotel.service.RoomService;
import icu.groot.alan.hotel.util.BizException;
import icu.groot.alan.hotel.util.ResultCodeEnum;
import icu.groot.alan.hotel.util.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * Room Management
 */
@RestController
@RequestMapping("/room")
public class RoomController {
    public static void main(String[] args) {
        String startTimeS = "2023-03-03";
        LocalDateTime startTime = LocalDateTime.parse(startTimeS + " 14:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String sendTimeS = "2023-03-04";
        LocalDateTime endTime = LocalDateTime.parse(sendTimeS + " 12:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
//        System.out.println(LocalDate.now().atStartOfDay());
//        System.out.println(startTime);
//        System.out.println(startTime.isAfter(LocalDate.now().atStartOfDay()));
//        System.out.println(startTime.toLocalDate().equals(LocalDateTime.now().toLocalDate()));
//        if (startTime.isBefore(LocalDateTime.now())){
        System.out.println();
    }
    @Resource
    RoomService roomService;

    @Resource
    OrderService orderService;

    @ApiOperation("Room List")
    @GetMapping({"/list"})
    public Result<List<RoomDo>> list(@RequestParam("checkinStartTime") String checkinStartTime, @RequestParam("checkinEndTime") String checkinEndTime){
        if (checkinStartTime == null || checkinStartTime.equals("") || checkinEndTime == null || checkinEndTime.equals("")){
            throw new BizException(ResultCodeEnum.FAILED.setMessage("请求参数不全"));
        }
        //转换日期格式
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startTime, endTime;
        try {
            startTime = LocalDateTime.parse(checkinStartTime + " 14:00:00", df);
            endTime = LocalDateTime.parse(checkinEndTime + " 12:00:00", df);
        }catch (Exception e){
            e.printStackTrace();
            throw new BizException(ResultCodeEnum.FAILED.setMessage("请求参数格式错误"));
        }
        // 判断日期要求是否合适
        if (startTime.isBefore(LocalDate.now().atStartOfDay()) || endTime.isBefore(startTime) || startTime.toLocalDate().equals(endTime.toLocalDate())){
            throw new BizException(ResultCodeEnum.FAILED.setMessage("选择的日期不对, 请重选"));
        }
        List<RoomDo> roomDos = roomService.list(new QueryWrapper<RoomDo>().eq("disable", false));
        List<OrderDo> orderDos = orderService.listByTime(startTime, endTime);
        for (OrderDo orderDo : orderDos) {
            for (RoomDo roomDo : roomDos) {
                if (orderDo.getRoomType().equals(roomDo.getRoomType()) && (orderDo.getOrderStatus().equals(OrderStatusEnum.REVERSE.getStatus()) || orderDo.getOrderStatus().equals(OrderStatusEnum.OCCUPANCY.getStatus()))){
                    roomDo.setRoomSpareNum(roomDo.getRoomSpareNum() - 1);
                }
            }
        }
        return Result.success(roomDos);
    }
}
